<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class CountModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $id=cmf_get_current_admin_id();
        $this->id=$id;
    }

    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
    public function index($cause)
    {
        /*条件编写*/
        $session=$this->session();
        if($session)
        {
            if($this->id == 1)
            {
                $where='';
                $cause['shop_id']='';
            }else{
                $where='or.shop_id="'.$session[0]['re_guid'].'"';
                $cause['shop_id']=$session[0]['re_guid'];
            }
            if(isset($cause['pay_type']))
            {
                if($cause['pay_type']>0)
                {
                    $where.=" and  or.pay_type =".$cause['pay_type'];
                }
            }else{
                $cause['pay_type']='';
            }
            if(isset($cause['order_no']))
            {
                if($cause['order_no'])
                {
                    $where .=" and or.order_no =".$cause['order_no'];
                }
            }else{
                $cause['order_no']='';
            }
            if(isset($cause['pay_status']))
            {
                if($cause['pay_status']!=0)
                {
                    $where.=" and or.pay_status = ".$cause['pay_status'];
                }
            }else{
                $cause['pay_status']='';
            }
            if(isset($cause['refunt']))
            {
                if($cause['refunt']!=0)
                {
                    $where.=" and or.refunt  = ".$cause['refunt'];
                }
            }else{
                $cause['refunt']='';
            }
            if(isset($cause['start_time']) && isset($cause['end_time']))
            {
                if(!empty($cause['start_time']))
                {
                    $start=strtotime($cause['start_time']);
                    if(!empty($cause['end_time']))
                    {
                        $end=strtotime($cause['end_time']);
                    }else{
                        $end=$start+86400;
                    }
                    $where.=' and or.w_time  >='.$start;
                    $where.=' and or.w_time  <='.$end;
                }
            }else{
                $cause['start_time']='';
                $cause['end_time']='';
            }
            $data=DB::name('order')->alias('or')->join(' member me ','me.m_guid = or.member_id')->join('shop sh','sh.re_guid = or.shop_id')->join('pay p','or.pay_type = p.pay_guid')->where($where)->field('or.*,me.nick_name,sh.shop_name,p.name')->paginate(10);
            /*对数据进行封装*/
            if($data->toArray())
            {

                foreach ($data as $k=>$v)
                {
                    $date=array();
                    $date=$v;
                    if($date['refunt']==1)
                    {
                        $date['refunt_name']='退款';
                    }else{
                        $date['refunt_name']='正常';
                    }
                    switch ($date['pay_status'])
                    {
                        case 1;
                            $date['pay_name']='支付';
                            break;
                        case 2;
                            $date['pay_name']='预约支付';
                            break;
                        case -3;
                            $date['pay_name']='未支付';
                            break;
                        case -1;
                            $date['pay_name']='挂单';
                            break;
                        case -2;
                            $date['pay_name']='订单关闭';
                            break;
                        default:
                            $date['pay_name']='订单出错';
                    }
                    $data->offsetSet($k,$date);

                }
            }
            /*分页保存条件*/
            $data->appends($cause);
            $array['data']=$data;
            $array['page']=$data->render();
        }else{
            $array['data']="";
            $array['page']="";
    }

        return $array;
    }
    /*支付方式查询*/
    public function pay($where=null,$state=null)
    {
        $session=$this->session();
        if($session)
        {
            if($this->id == 1)
            {
                $where='';
            }else{
                $where['shop_id']=$session[0]['re_guid'];
            }
            $where['status']=1;
            $category=DB::name('pay')->where($where)->select()->toArray();
            $data['id']=0;
            $data['name']='全部';
            if($state == 1)
            {
                if(!isset($where['id']))
                {
                    foreach ($category as $k=>$v)
                    {
                        $category[0]=$data;
                        $category[$k+1]=$v;
                    }
                }
            }
        }else{
           $category="";
        }
        return $category;
    }
    /*查询总金额*/
    public function total($cause)
    {
        $session=$this->session();
        if($session)
        {
            if($this->id==1)
            {
                $where="";
            }else{
                $where['shop_id']=$session[0]['re_guid'];
            }
            if(isset($cause['pay_type']))
            {
                if($cause['pay_type']>0)
                {
                    $where['pay_type']=$cause['pay_type'];
                }
            }
            if(isset($cause['order_no']))
            {
                if($cause['order_no'])
                {
                    $where['order_no']=$cause['order_no'];
                }
            }
            if(isset($cause['pay_status']))
            {
                if($cause['pay_status']!=0)
                {
                    $where['pay_status']=$cause['pay_status'];
                }
            }
            if(isset($cause['refunt']))
            {
                if($cause['refunt']!=0)
                {
                    $where['refunt']=$cause['refunt'];
                }
            }
            $total_money=DB::name('order')->where($where)->field('sum(total_money) as total_money,sum(actual_money) as actual_money')->select()->toArray();
        }else{
            $total_money=0;
        }
        return $total_money;
    }
    /*查询商品详情*/
    public function details($array)
    {
       $session=$this->session();
       if($session)
       {
           if($this->id==1)
           {
               $where="";
           }else{
               $where['od.shop_id']=$session[0]['re_guid'];
           }
           $where['od.order_num']=$array['id'];
           $date=DB::name('order_detail')->alias('od')->join('format f','f.f_id = od.for_matid')->where($where)->field('od.*,f.f_name')->paginate(10);
           if($date->toArray())
           {
               $date->toArray();
               foreach($date as $k=>$v)
               {
                   $data=array();
                   $data=$v;
                   if($data['nature']!=0)
                   {
                       $nature=DB::name('goods_backup')->where('id',$data['nature'])->field('state_name')->find();
                       $data['nature_name']=$nature['state_name'];
                   }else{
                       $data['nature_name']='无';
                   }
                   if($data['integral']==1)
                   {
                       $data['integral_name']='积分';
                   }else{
                       $data['integral_name']='正常';
                   }
                   if($data['stemeal']==1)
                   {
                       $data['stemeal_name']='单品';
                   }else{
                       $data['stemeal_name']='套餐';
                   }
                   $date->offsetSet($k,$data);
               }
           }
           $array['data']=$date;
           $array['page']=$date->render();
       }else{
           $array['data']="";
           $array['page']="";
       }

       return $array;
    }
}